home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / protocols / adapters.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  160 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __all__ = [
  5.     'NO_ADAPTER_NEEDED',
  6.     'DOES_NOT_SUPPORT',
  7.     'Adapter',
  8.     'minimumAdapter',
  9.     'composeAdapters',
  10.     'updateWithSimplestAdapter',
  11.     'StickyAdapter',
  12.     'AdaptationFailure',
  13.     'bindAdapter']
  14. from types import FunctionType, ClassType, MethodType
  15.  
  16. try:
  17.     PendingDeprecationWarning
  18. except NameError:
  19.     
  20.     class PendingDeprecationWarning(Warning):
  21.         pass
  22.  
  23.  
  24.  
  25. class AdaptationFailure(NotImplementedError, TypeError):
  26.     pass
  27.  
  28.  
  29. def NO_ADAPTER_NEEDED(obj, protocol = None):
  30.     return obj
  31.  
  32.  
  33. def DOES_NOT_SUPPORT(obj, protocol = None):
  34.     pass
  35.  
  36.  
  37. try:
  38.     from _speedups import NO_ADAPTER_NEEDED, DOES_NOT_SUPPORT
  39. except ImportError:
  40.     pass
  41.  
  42.  
  43. class Adapter(object):
  44.     
  45.     def __init__(self, ob):
  46.         self.subject = ob
  47.  
  48.  
  49.  
  50. class StickyAdapter(object):
  51.     attachForProtocols = ()
  52.     
  53.     def __init__(self, ob):
  54.         self.subject = ob
  55.         provides = list(self.attachForProtocols)
  56.         declareAdapter = declareAdapter
  57.         import protocols.api
  58.         (declareAdapter,)((lambda s: self), provides, forObjects = [
  59.             ob])
  60.  
  61.  
  62.  
  63. def minimumAdapter(a1, a2, d1 = 0, d2 = 0):
  64.     if d1 < d2:
  65.         return a1
  66.     elif d2 < d1:
  67.         return a2
  68.     
  69.     if getattr(a1, '__unbound_adapter__', a1) is getattr(a2, '__unbound_adapter__', a2):
  70.         return a1
  71.     
  72.     a1ct = getattr(a1, '__adapterCount__', 1)
  73.     a2ct = getattr(a2, '__adapterCount__', 1)
  74.     if a1ct < a2ct:
  75.         return a1
  76.     elif a2ct < a1ct:
  77.         return a2
  78.     
  79.     if a1 is NO_ADAPTER_NEEDED or a2 is DOES_NOT_SUPPORT:
  80.         return a1
  81.     
  82.     if a1 is DOES_NOT_SUPPORT or a2 is NO_ADAPTER_NEEDED:
  83.         return a2
  84.     
  85.     raise TypeError('Ambiguous adapter choice', a1, a2, d1, d2)
  86.  
  87.  
  88. def composeAdapters(baseAdapter, baseProtocol, extendingAdapter):
  89.     if baseAdapter is DOES_NOT_SUPPORT or extendingAdapter is DOES_NOT_SUPPORT:
  90.         return DOES_NOT_SUPPORT
  91.     
  92.     if baseAdapter is NO_ADAPTER_NEEDED:
  93.         return extendingAdapter
  94.     
  95.     if extendingAdapter is NO_ADAPTER_NEEDED:
  96.         return baseAdapter
  97.     
  98.     
  99.     def newAdapter(ob):
  100.         ob = baseAdapter(ob)
  101.         if ob is not None:
  102.             return extendingAdapter(ob)
  103.         
  104.  
  105.     newAdapter.__adapterCount__ = getattr(extendingAdapter, '__adapterCount__', 1) + getattr(baseAdapter, '__adapterCount__', 1)
  106.     return newAdapter
  107.  
  108.  
  109. def bindAdapter(adapter, proto):
  110.     maxargs = 2
  111.     f = adapter
  112.     tries = 10
  113.     while not isinstance(f, FunctionType) and tries:
  114.         if isinstance(f, MethodType):
  115.             maxargs += (f.im_self is not None)
  116.             f = f.im_func
  117.             tries = 10
  118.             continue
  119.         if isinstance(f, (ClassType, type)):
  120.             maxargs += 1
  121.             f = f.__init__
  122.             tries -= 1
  123.             continue
  124.         f = f.__call__
  125.         tries -= 1
  126.     if isinstance(f, FunctionType):
  127.         getargspec = getargspec
  128.         import inspect
  129.         (args, varargs, varkw, defaults) = getargspec(f)
  130.         if defaults:
  131.             args = args[:-len(defaults)]
  132.         
  133.         if len(args) >= maxargs:
  134.             
  135.             newAdapter = lambda ob: adapter(ob, proto)
  136.             newAdapter.__adapterCount__ = getattr(adapter, '__adapterCount__', 1)
  137.             newAdapter.__unbound_adapter__ = adapter
  138.             warn = warn
  139.             import warnings
  140.             warn('Adapter %r to protocol %r needs multiple arguments' % (adapter, proto), PendingDeprecationWarning, 6)
  141.             return newAdapter
  142.         
  143.     
  144.     return adapter
  145.  
  146.  
  147. def updateWithSimplestAdapter(mapping, key, adapter, depth):
  148.     new = adapter
  149.     old = mapping.get(key)
  150.     if old is not None:
  151.         (old, oldDepth) = old
  152.         new = minimumAdapter(old, adapter, oldDepth, depth)
  153.         if old is new and depth >= oldDepth:
  154.             return False
  155.         
  156.     
  157.     mapping[key] = (new, depth)
  158.     return True
  159.  
  160.